package com.anolesoft.Planexecution.controller;

import com.anolesoft.Planexecution.dto.MesBzzOrderDto;
import com.anolesoft.Planexecution.service.MesBzzOrderService;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * liuqirui
 */

@RestController
@RequestMapping(value="/mes")
public class MesBzzOrderController {

    @Autowired
    private MesBzzOrderService mesBzzOrderService;

    /**
     * 查询
     * @param mesBzzOrderDto
     * @return
     */
    @PostMapping(value = "/queryAll")
    public AjaxResult findAll(@RequestBody MesBzzOrderDto mesBzzOrderDto) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();

        MesBzzOrderDto mesBzzOrderDto1 = new MesBzzOrderDto();
        Integer pageNo = mesBzzOrderDto.getPageNo();
        Integer pageSize = mesBzzOrderDto.getPageSize();
        mesBzzOrderDto1.setFactory(loginUser.getFacCode());

        //条码号
        if(mesBzzOrderDto.getBarCode() != null){
            String barCode = mesBzzOrderDto.getBarCode().trim();
            mesBzzOrderDto1.setBarCode(barCode);
            System.out.println(barCode);
        }
        //派工号
        if(mesBzzOrderDto.getPaigonghao() != null){
            String paigonghao = mesBzzOrderDto.getPaigonghao().trim();
            mesBzzOrderDto1.setPaigonghao(paigonghao);
        }
        //状态
        if(mesBzzOrderDto.getState() != null) {
            Integer state = mesBzzOrderDto.getState();
            mesBzzOrderDto1.setState(state);
        }

        List<MesBzzOrderDto> allList = mesBzzOrderService.queryAll(mesBzzOrderDto1);
        int endIndex = pageNo * pageSize > allList.size() ? allList.size() : pageNo * pageSize;
        List<MesBzzOrderDto> list = allList.subList( (pageNo-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNo, allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    /**
     * 修改状态
     * @param list
     * @return
     */
    @PostMapping(value = "/updateState")
    public AjaxResult update(@RequestBody List<String> list){
        // 加归档人
        HashMap map = new HashMap();
        map.put("list",list);
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String objuid = loginUser.getEmpUid();
        map.put("filedby",objuid);
        // 传入map
        int a = mesBzzOrderService.update(map);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }



}
